#define _GNU_SOURCE
#include "ax_printf.h"
#include "ax_timer.h"
#include <stdio.h>

static void timer_fun1(int tid, int param1, int param2)
{
    printf("timer_fun1 fire. tid:%d param1:%d param2:%d\n", tid, param1, param2);
}

static void timer_fun2(int tid, int param1, int param2)
{
    printf("timer_fun2 fire. tid:%d param1:%d param2:%d\n", tid, param1, param2);
}

int test_timer_loop(bool loop)
{
    ax_timer_mgr_init(loop);

    int tid1 = ax_timer_create(timer_fun1);
    int tid2 = ax_timer_create(timer_fun2);

    ax_timer_start(tid1, 2000, en_TIMER_NOLOOP, 1, 2);
    ax_timer_start(tid2, 1000, en_TIMER_LOOP, 3, 4);

    if(!loop)
    {
        while(true)
        {
            ax_timer_main();
        }
    }

    ax_debug("hit to test timer stop..\n");
    getchar();
    ax_timer_stop(tid2);

    ax_debug("hit to test timer restart...\n");
    getchar();
    ax_timer_start(tid2, 1000, en_TIMER_LOOP, 3, 4);

    ax_debug("hit to test timer reset...\n");
    getchar();
    ax_timer_reset(tid2);

    ax_debug("hit to test no loop timer start once more...\n");
    getchar();
    ax_timer_start(tid1, 2000, en_TIMER_NOLOOP, 1, 2);

    ax_debug("hit to test no loop timer reset...\n");
    getchar();
    ax_timer_reset(tid1);

    ax_debug("hit to exit..");
    getchar();

    ax_timer_mgr_free();
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <unistd.h>
#ifndef MAX_LEN
#define MAX_LEN 256
#endif
#define EPOLL_SIZE 1000

int test_epoll(int atgc, char** argv)
{
    int                fd           = 0;
    int                len          = 0;
    int                nfds         = 0;
    int                index        = 0;
    char               buf[MAX_LEN] = { 0 };
    struct epoll_event ev;
    struct epoll_event event[EPOLL_SIZE];

    // create a epoll object
    if(-1 == (fd = epoll_create(EPOLL_SIZE)))
    {
        perror("create epoll object error:");
        return -1;
    }

    // add a file descriptor which need to be listened
    ev.events  = EPOLLIN | EPOLLET;
    ev.data.fd = STDIN_FILENO;
    epoll_ctl(fd, EPOLL_CTL_ADD, STDIN_FILENO, &ev);

    // begin to listen the file descriptor
    while(1)
    {
        nfds = epoll_wait(fd, event, 1000, -1);
        if(nfds == -1)
        {
            perror("epoll_wait:");
            return -1;
        }
        for(index = 0; index < nfds; index++)
        {
            if(event[index].events == EPOLLIN)
            {
                len      = read(event[index].data.fd, buf, sizeof(buf));
                buf[len] = '\0';
                printf("buf = %s\n", buf);
            }
        }
    }

    return EXIT_SUCCESS;
}
int main(int argc, char** argv)
{
    return test_epoll(argc, argv);
    return test_timer_loop(true);
}
